<script setup>
  import { ref } from 'vue'
  import { Delete, Edit } from '@element-plus/icons-vue'
  import ChannelSelect from './components/ChannelSelect.vue'
 import ArticleEdit from './components/ArticleEdit.vue'
  import { artGetListService,artDelService } from '@/api/article'
  import { formatTime } from '@/utils/format.js'
  const articleList = ref([])
  const total = ref(0)
  const loading = ref(false)
  const drawer = ref()
  //  定义请求参数对象
  const params = ref({
    pagenum: 1,
    pagesize: 5,
    cate_id: '',
    state: ''
  })

  const getArticleList = async () => {
    loading.value=true
    const res = await artGetListService(params.value)
    articleList.value = res.data.data
    total.value = res.data.total
    loading.value=false
  }
  getArticleList()
  // 编辑逻辑
  const onEditArticle = row => {
    drawer.value.open(row)
  }
  // 删除逻辑
  const onDeleteArticle =  async(row) => {
    await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
    await artDelService(row.id)
    ElMessage.success('删除成功')
    getArticleList()
  }
  // 添加逻辑
  const onAddArticle = ()=>{
    drawer.value.open({})
  }
  // 处理分页逻辑
  const onSizeChange = (size) => {
    // console.log('当前每页条数',size);
    // 只要是每页条数变化了，那么原本正在访问的当前页意义不大了，数据大概率不在原来的那一页了
    // 重新从第一页渲染即可
    params.value.pagenum = 1
    params.value.size = size
    // 基于最新的当前页和每页条数渲染数据
    getArticleList()

  }
  const onCurrentChange = page => {
    params.value.pagenum = page
    // 基于最新的当前页和每页条数渲染数据
    getArticleList()
  }
  // 搜索
  const onSearch = ()=>{
    params.value.pagenum=1
    getArticleList()
  }
  // 重置
  const onReset = ()=>{
    params.value.pagenum=1
    params.value.size = 5
    params.value.cate_id = ''
    params.value.state=''
    getArticleList()
  }
  // 添加或者编辑成功的回调
  const onSuccess = type =>{
    if(type==='add'){
      // 如果是添加渲染最后一页
      const lastPage = Math.ceil((total.value+1)/params.value.pagesize)
      // 更新成最大页码数，再渲染
      params.value.pagenum = lastPage

      getArticleList()
    }else{
      // 如果是编辑，直接渲染当前页面即可
      getArticleList()
    }
  }
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">添加文章</el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="文章分类:  " style="width: 300px;">
        <!-- vue2 => v-model :value 和@input的简写 -->
        <!-- vue3 => v-model :modelValue 和@update:modelValue的简写 -->
        <!-- v-model:cid :cid和@update:cid的简写 -->
        <ChannelSelect v-model="params.cate_id" width='60%'></ChannelSelect>
        <!-- 这里后台标记发布状态，就是通过中文标记的，已发布/草稿 -->
      </el-form-item>
      <el-form-item label="发布状态:  ">
        <el-select v-model="params.state" style="width: 200px;">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column prop="title" label="文章标题">
        <template #default="{row}">
          <el-link type="primary" :underline="false">{{row.title}}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="分类"></el-table-column>
      <el-table-column prop="pub_date" label="发布时间">
        <template #default="{row}">
          {{formatTime(row.pub_date)}}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态"></el-table-column>
      <!-- 利用作用域插槽 row 可以获取当前行的数据=》 v-for item -->
      <el-table-column prop="" label="操作">
        <template #default="{row}">
          <el-button cricle plain type="primary" :icon="Edit" @click="onEditArticle(row)">

          </el-button>
          <el-button cricle plain type="danger" :icon="Delete" @click="onDeleteArticle(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <div class="demo-pagination-block">
      <el-pagination v-model:current-page="params.pagenum" v-model:page-size="params.size" :page-sizes="[2, 3, 5, 10]"
        :background="true" layout="jumper,total, sizes, prev, pager, next," :total="total" @size-change="onSizeChange"
        @current-change="onCurrentChange" style="margin-top: 20px;justify-content: flex-end;" />
    </div>
    <ArticleEdit ref="drawer" @success="onSuccess"></ArticleEdit>
  </page-container>
</template>
<style lang="scss" scoped>

</style>